<?php
require_once  '../classes/UtenteManager.php';
require_once  '../classes/ConnectionDB.php';
$operation = $_POST["operation"];
if($operation == "updateUser")
{
    $codice = $_POST["codice"];
    $attivo = $_POST["attivo"];

    $attivo = ($attivo=="on")?1:0;

    $username = $_POST["username"];
    $nome = $_POST["nome"];
    $cognome = $_POST["cognome"];
    $email = $_POST["email"];

     $errors = array();

    if ($username == "")
        $errors[] = '1';

    if ($nome == "")
        $errors[] = '3';
    if ($cognome == "")
        $errors[] = '4';
    if ($email == "")
        $errors[] = '5';


    if (count($errors) > 0)
    {
        $resp = ErrorsManager::getInstance()->manageErrors($errors);
        header("Location:../portal/manageErrors.php?lista=" . $errors);
    }

    $connection =ConnectionDB::getInstance()->connectToDB();
    UtenteManager::getInstance()->updateUtente($connection, $codice, $attivo, $username, $nome, $cognome, $email);
    header("Location:utente.php?codiceUtente=" . $codice);
}
if ($operation== "updatePassword")
{
    $codice = $_POST["codice"];
    $oldPassword =  $_POST["oldPassword"];
    $newPassword =  $_POST["newPassword"];
    $connection =ConnectionDB::getInstance()->connectToDB();
    $resultOperation = UtenteManager::getInstance()->updatePassword($connection,$codice, $oldPassword, $newPassword);
    header("Location:utente.php?codiceUtente=" . $codice);
}
?>
